﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.LogNet;
using Newtonsoft.Json.Linq;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 包含了主动异步接收的方法实现和文件类异步读写的实现<br />
	/// Contains the implementation of the active asynchronous receiving method and the implementation of asynchronous reading and writing of the file class
	/// </summary>
	// Token: 0x02000142 RID: 322
	public class NetworkXBase : NetworkBase
	{
		/// <summary>
		/// [自校验] 将文件数据发送至套接字，如果结果异常，则结束通讯<br />
		/// [Self-check] Send the file data to the socket. If the result is abnormal, the communication is ended.
		/// </summary>
		/// <param name="socket">网络套接字</param>
		/// <param name="filename">完整的文件路径</param>
		/// <param name="filelength">文件的长度</param>
		/// <param name="report">进度报告器</param>
		/// <returns>是否发送成功</returns>
		// Token: 0x06001C41 RID: 7233 RVA: 0x0008B828 File Offset: 0x00089A28
		protected OperateResult SendFileStreamToSocket(Socket socket, string filename, long filelength, Action<long, long> report = null)
		{
			OperateResult result;
			try
			{
				OperateResult operateResult = new OperateResult();
				using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
				{
					operateResult = base.SendStreamToSocket(socket, fileStream, filelength, report, true);
				}
				result = operateResult;
			}
			catch (Exception ex)
			{
				if (socket != null)
				{
					socket.Close();
				}
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), ex);
				}
				result = new OperateResult(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// [自校验] 将文件数据发送至套接字，具体发送细节将在继承类中实现，如果结果异常，则结束通讯<br />
		/// [Self-checking] Send the file data to the socket. The specific sending details will be implemented in the inherited class. If the result is abnormal, the communication will end
		/// </summary>
		/// <param name="socket">套接字</param>
		/// <param name="filename">文件名称，文件必须存在</param>
		/// <param name="servername">远程端的文件名称</param>
		/// <param name="filetag">文件的额外标签</param>
		/// <param name="fileupload">文件的上传人</param>
		/// <param name="sendReport">发送进度报告</param>
		/// <returns>是否发送成功</returns>
		// Token: 0x06001C42 RID: 7234 RVA: 0x0008B8B8 File Offset: 0x00089AB8
		protected OperateResult SendFileAndCheckReceive(Socket socket, string filename, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
		{
			FileInfo fileInfo = new FileInfo(filename);
			bool flag = !File.Exists(filename);
			OperateResult result;
			if (flag)
			{
				OperateResult operateResult = base.SendStringAndCheckReceive(socket, 0, "");
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					if (socket != null)
					{
						socket.Close();
					}
					result = new OperateResult(StringResources.Language.FileNotExist);
				}
			}
			else
			{
				JObject jobject = new JObject();
				jobject.Add("FileName", new JValue(servername));
				jobject.Add("FileSize", new JValue(fileInfo.Length));
				jobject.Add("FileTag", new JValue(filetag));
				jobject.Add("FileUpload", new JValue(fileupload));
				JObject jobject2 = jobject;
				OperateResult operateResult2 = base.SendStringAndCheckReceive(socket, 1, jobject2.ToString());
				bool flag3 = !operateResult2.IsSuccess;
				if (flag3)
				{
					result = operateResult2;
				}
				else
				{
					result = this.SendFileStreamToSocket(socket, filename, fileInfo.Length, sendReport);
				}
			}
			return result;
		}

		/// <summary>
		/// [自校验] 将流数据发送至套接字，具体发送细节将在继承类中实现，如果结果异常，则结束通讯<br />
		/// [Self-checking] Send stream data to the socket. The specific sending details will be implemented in the inherited class. 
		/// If the result is abnormal, the communication will be terminated
		/// </summary>
		/// <param name="socket">套接字</param>
		/// <param name="stream">文件名称，文件必须存在</param>
		/// <param name="servername">远程端的文件名称</param>
		/// <param name="filetag">文件的额外标签</param>
		/// <param name="fileupload">文件的上传人</param>
		/// <param name="sendReport">发送进度报告</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001C43 RID: 7235 RVA: 0x0008B9B4 File Offset: 0x00089BB4
		protected OperateResult SendFileAndCheckReceive(Socket socket, Stream stream, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
		{
			JObject jobject = new JObject();
			jobject.Add("FileName", new JValue(servername));
			jobject.Add("FileSize", new JValue(stream.Length));
			jobject.Add("FileTag", new JValue(filetag));
			jobject.Add("FileUpload", new JValue(fileupload));
			JObject jobject2 = jobject;
			OperateResult operateResult = base.SendStringAndCheckReceive(socket, 1, jobject2.ToString());
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = base.SendStreamToSocket(socket, stream, stream.Length, sendReport, true);
			}
			return result;
		}

		/// <summary>
		/// [自校验] 从套接字中接收文件头信息<br />
		/// [Self-checking] Receive file header information from socket
		/// </summary>
		/// <param name="socket">套接字的网络</param>
		/// <returns>包含文件信息的结果对象</returns>
		// Token: 0x06001C44 RID: 7236 RVA: 0x0008BA4C File Offset: 0x00089C4C
		protected OperateResult<FileBaseInfo> ReceiveFileHeadFromSocket(Socket socket)
		{
			OperateResult<int, string> operateResult = base.ReceiveStringContentFromSocket(socket, 30000);
			bool flag = !operateResult.IsSuccess;
			OperateResult<FileBaseInfo> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<FileBaseInfo>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content1 == 0;
				if (flag2)
				{
					if (socket != null)
					{
						socket.Close();
					}
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteWarn(this.ToString(), StringResources.Language.FileRemoteNotExist);
					}
					result = new OperateResult<FileBaseInfo>(StringResources.Language.FileNotExist);
				}
				else
				{
					OperateResult<FileBaseInfo> operateResult2 = new OperateResult<FileBaseInfo>
					{
						Content = new FileBaseInfo()
					};
					try
					{
						JObject json = JObject.Parse(operateResult.Content2);
						operateResult2.Content.Name = SoftBasic.GetValueFromJsonObject<string>(json, "FileName", "");
						operateResult2.Content.Size = SoftBasic.GetValueFromJsonObject<long>(json, "FileSize", 0L);
						operateResult2.Content.Tag = SoftBasic.GetValueFromJsonObject<string>(json, "FileTag", "");
						operateResult2.Content.Upload = SoftBasic.GetValueFromJsonObject<string>(json, "FileUpload", "");
						operateResult2.IsSuccess = true;
					}
					catch (Exception ex)
					{
						if (socket != null)
						{
							socket.Close();
						}
						operateResult2.Message = "Extra File Head Wrong:" + ex.Message;
					}
					result = operateResult2;
				}
			}
			return result;
		}

		/// <summary>
		/// [自校验] 从网络中接收一个文件，如果结果异常，则结束通讯<br />
		/// [Self-checking] Receive a file from the network. If the result is abnormal, the communication ends.
		/// </summary>
		/// <param name="socket">网络套接字</param>
		/// <param name="savename">接收文件后保存的文件名</param>
		/// <param name="receiveReport">接收进度报告</param>
		/// <returns>包含文件信息的结果对象</returns>
		// Token: 0x06001C45 RID: 7237 RVA: 0x0008BBB0 File Offset: 0x00089DB0
		protected OperateResult<FileBaseInfo> ReceiveFileFromSocket(Socket socket, string savename, Action<long, long> receiveReport)
		{
			OperateResult<FileBaseInfo> operateResult = this.ReceiveFileHeadFromSocket(socket);
			bool flag = !operateResult.IsSuccess;
			OperateResult<FileBaseInfo> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				try
				{
					OperateResult operateResult2 = null;
					using (FileStream fileStream = new FileStream(savename, FileMode.Create, FileAccess.Write))
					{
						operateResult2 = base.WriteStreamFromSocket(socket, fileStream, operateResult.Content.Size, receiveReport, true);
					}
					bool flag2 = !operateResult2.IsSuccess;
					if (flag2)
					{
						bool flag3 = File.Exists(savename);
						if (flag3)
						{
							File.Delete(savename);
						}
						result = OperateResult.CreateFailedResult<FileBaseInfo>(operateResult2);
					}
					else
					{
						result = operateResult;
					}
				}
				catch (Exception ex)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteException(this.ToString(), ex);
					}
					if (socket != null)
					{
						socket.Close();
					}
					result = new OperateResult<FileBaseInfo>
					{
						Message = ex.Message
					};
				}
			}
			return result;
		}

		/// <summary>
		/// [自校验] 从网络中接收一个文件，写入数据流，如果结果异常，则结束通讯，参数顺序文件名，文件大小，文件标识，上传人<br />
		/// [Self-checking] Receive a file from the network. If the result is abnormal, the communication ends.
		/// </summary>
		/// <param name="socket">网络套接字</param>
		/// <param name="stream">等待写入的数据流</param>
		/// <param name="receiveReport">接收进度报告</param>
		/// <returns>文件头结果</returns>
		// Token: 0x06001C46 RID: 7238 RVA: 0x0008BC9C File Offset: 0x00089E9C
		protected OperateResult<FileBaseInfo> ReceiveFileFromSocket(Socket socket, Stream stream, Action<long, long> receiveReport)
		{
			OperateResult<FileBaseInfo> operateResult = this.ReceiveFileHeadFromSocket(socket);
			bool flag = !operateResult.IsSuccess;
			OperateResult<FileBaseInfo> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				try
				{
					base.WriteStreamFromSocket(socket, stream, operateResult.Content.Size, receiveReport, true);
					result = operateResult;
				}
				catch (Exception ex)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteException(this.ToString(), ex);
					}
					if (socket != null)
					{
						socket.Close();
					}
					result = new OperateResult<FileBaseInfo>
					{
						Message = ex.Message
					};
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.SendFileStreamToSocket(System.Net.Sockets.Socket,System.String,System.Int64,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06001C47 RID: 7239 RVA: 0x0008BD2C File Offset: 0x00089F2C
		[DebuggerStepThrough]
		protected Task<OperateResult> SendFileStreamToSocketAsync(Socket socket, string filename, long filelength, Action<long, long> report = null)
		{
			NetworkXBase.<SendFileStreamToSocketAsync>d__7 <SendFileStreamToSocketAsync>d__ = new NetworkXBase.<SendFileStreamToSocketAsync>d__7();
			<SendFileStreamToSocketAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SendFileStreamToSocketAsync>d__.<>4__this = this;
			<SendFileStreamToSocketAsync>d__.socket = socket;
			<SendFileStreamToSocketAsync>d__.filename = filename;
			<SendFileStreamToSocketAsync>d__.filelength = filelength;
			<SendFileStreamToSocketAsync>d__.report = report;
			<SendFileStreamToSocketAsync>d__.<>1__state = -1;
			<SendFileStreamToSocketAsync>d__.<>t__builder.Start<NetworkXBase.<SendFileStreamToSocketAsync>d__7>(ref <SendFileStreamToSocketAsync>d__);
			return <SendFileStreamToSocketAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.SendFileAndCheckReceive(System.Net.Sockets.Socket,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06001C48 RID: 7240 RVA: 0x0008BD90 File Offset: 0x00089F90
		[DebuggerStepThrough]
		protected Task<OperateResult> SendFileAndCheckReceiveAsync(Socket socket, string filename, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
		{
			NetworkXBase.<SendFileAndCheckReceiveAsync>d__8 <SendFileAndCheckReceiveAsync>d__ = new NetworkXBase.<SendFileAndCheckReceiveAsync>d__8();
			<SendFileAndCheckReceiveAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SendFileAndCheckReceiveAsync>d__.<>4__this = this;
			<SendFileAndCheckReceiveAsync>d__.socket = socket;
			<SendFileAndCheckReceiveAsync>d__.filename = filename;
			<SendFileAndCheckReceiveAsync>d__.servername = servername;
			<SendFileAndCheckReceiveAsync>d__.filetag = filetag;
			<SendFileAndCheckReceiveAsync>d__.fileupload = fileupload;
			<SendFileAndCheckReceiveAsync>d__.sendReport = sendReport;
			<SendFileAndCheckReceiveAsync>d__.<>1__state = -1;
			<SendFileAndCheckReceiveAsync>d__.<>t__builder.Start<NetworkXBase.<SendFileAndCheckReceiveAsync>d__8>(ref <SendFileAndCheckReceiveAsync>d__);
			return <SendFileAndCheckReceiveAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.SendFileAndCheckReceive(System.Net.Sockets.Socket,System.IO.Stream,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06001C49 RID: 7241 RVA: 0x0008BE04 File Offset: 0x0008A004
		[DebuggerStepThrough]
		protected Task<OperateResult> SendFileAndCheckReceiveAsync(Socket socket, Stream stream, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
		{
			NetworkXBase.<SendFileAndCheckReceiveAsync>d__9 <SendFileAndCheckReceiveAsync>d__ = new NetworkXBase.<SendFileAndCheckReceiveAsync>d__9();
			<SendFileAndCheckReceiveAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SendFileAndCheckReceiveAsync>d__.<>4__this = this;
			<SendFileAndCheckReceiveAsync>d__.socket = socket;
			<SendFileAndCheckReceiveAsync>d__.stream = stream;
			<SendFileAndCheckReceiveAsync>d__.servername = servername;
			<SendFileAndCheckReceiveAsync>d__.filetag = filetag;
			<SendFileAndCheckReceiveAsync>d__.fileupload = fileupload;
			<SendFileAndCheckReceiveAsync>d__.sendReport = sendReport;
			<SendFileAndCheckReceiveAsync>d__.<>1__state = -1;
			<SendFileAndCheckReceiveAsync>d__.<>t__builder.Start<NetworkXBase.<SendFileAndCheckReceiveAsync>d__9>(ref <SendFileAndCheckReceiveAsync>d__);
			return <SendFileAndCheckReceiveAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.ReceiveFileHeadFromSocket(System.Net.Sockets.Socket)" />
		// Token: 0x06001C4A RID: 7242 RVA: 0x0008BE78 File Offset: 0x0008A078
		[DebuggerStepThrough]
		protected Task<OperateResult<FileBaseInfo>> ReceiveFileHeadFromSocketAsync(Socket socket)
		{
			NetworkXBase.<ReceiveFileHeadFromSocketAsync>d__10 <ReceiveFileHeadFromSocketAsync>d__ = new NetworkXBase.<ReceiveFileHeadFromSocketAsync>d__10();
			<ReceiveFileHeadFromSocketAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
			<ReceiveFileHeadFromSocketAsync>d__.<>4__this = this;
			<ReceiveFileHeadFromSocketAsync>d__.socket = socket;
			<ReceiveFileHeadFromSocketAsync>d__.<>1__state = -1;
			<ReceiveFileHeadFromSocketAsync>d__.<>t__builder.Start<NetworkXBase.<ReceiveFileHeadFromSocketAsync>d__10>(ref <ReceiveFileHeadFromSocketAsync>d__);
			return <ReceiveFileHeadFromSocketAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.ReceiveFileFromSocket(System.Net.Sockets.Socket,System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06001C4B RID: 7243 RVA: 0x0008BEC4 File Offset: 0x0008A0C4
		[DebuggerStepThrough]
		protected Task<OperateResult<FileBaseInfo>> ReceiveFileFromSocketAsync(Socket socket, string savename, Action<long, long> receiveReport)
		{
			NetworkXBase.<ReceiveFileFromSocketAsync>d__11 <ReceiveFileFromSocketAsync>d__ = new NetworkXBase.<ReceiveFileFromSocketAsync>d__11();
			<ReceiveFileFromSocketAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
			<ReceiveFileFromSocketAsync>d__.<>4__this = this;
			<ReceiveFileFromSocketAsync>d__.socket = socket;
			<ReceiveFileFromSocketAsync>d__.savename = savename;
			<ReceiveFileFromSocketAsync>d__.receiveReport = receiveReport;
			<ReceiveFileFromSocketAsync>d__.<>1__state = -1;
			<ReceiveFileFromSocketAsync>d__.<>t__builder.Start<NetworkXBase.<ReceiveFileFromSocketAsync>d__11>(ref <ReceiveFileFromSocketAsync>d__);
			return <ReceiveFileFromSocketAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkXBase.ReceiveFileFromSocket(System.Net.Sockets.Socket,System.IO.Stream,System.Action{System.Int64,System.Int64})" />
		// Token: 0x06001C4C RID: 7244 RVA: 0x0008BF20 File Offset: 0x0008A120
		[DebuggerStepThrough]
		protected Task<OperateResult<FileBaseInfo>> ReceiveFileFromSocketAsync(Socket socket, Stream stream, Action<long, long> receiveReport)
		{
			NetworkXBase.<ReceiveFileFromSocketAsync>d__12 <ReceiveFileFromSocketAsync>d__ = new NetworkXBase.<ReceiveFileFromSocketAsync>d__12();
			<ReceiveFileFromSocketAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
			<ReceiveFileFromSocketAsync>d__.<>4__this = this;
			<ReceiveFileFromSocketAsync>d__.socket = socket;
			<ReceiveFileFromSocketAsync>d__.stream = stream;
			<ReceiveFileFromSocketAsync>d__.receiveReport = receiveReport;
			<ReceiveFileFromSocketAsync>d__.<>1__state = -1;
			<ReceiveFileFromSocketAsync>d__.<>t__builder.Start<NetworkXBase.<ReceiveFileFromSocketAsync>d__12>(ref <ReceiveFileFromSocketAsync>d__);
			return <ReceiveFileFromSocketAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001C4D RID: 7245 RVA: 0x0008BF79 File Offset: 0x0008A179
		public override string ToString()
		{
			return "NetworkXBase";
		}
	}
}
